package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageBean;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping ("/admin/category")
@Api (tags = "分类相关接口")
@Slf4j
@AllArgsConstructor
public class CategoryController {
    private final CategoryService categoryService;

    /**
     * 修改分类
     *
     * @param categoryDTO 修改
     * @return Result
     */
    @ApiOperation (value = "修改分类")
    @PutMapping
    public Result updateCategory(@RequestBody CategoryDTO categoryDTO) {
        categoryService.updateCategory(categoryDTO);
        return Result.success();
    }

    /**
     * 分页查询
     *
     * @param categoryPageQueryDTO 条件
     * @return Result
     */
    @ApiOperation (value = "分类分页查询")
    @GetMapping ("/page")
    public Result pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageBean pageBean = categoryService.pageQuery(categoryPageQueryDTO);
        return Result.success(pageBean);
    }

    /**
     * 修改状态
     *
     * @param status   当前状态
     * @param category 分类id
     * @return R
     */
    @ApiOperation (value = "修改状态")
    @PostMapping ("/status/{status}")
    public Result updateState(@PathVariable Integer status, Category category) {
        categoryService.updateState(status, category);
        return Result.success();
    }

    /**
     * 添加分类
     * @param category 添加信息
     * @return R
     */
    @ApiOperation(value = "添加分类")
    @PostMapping
    public Result addCategory(@RequestBody Category category){
        categoryService.addCategory(category);
        return Result.success();
    }

    /**
     * 根据id删除
     * @param category 主键
     * @return R
     */
    @ApiOperation(value = "根据id删除")
    @DeleteMapping
    public Result deleteById(Category category){
        categoryService.deleteById(category);
        return Result.success();
    }

    /**
     * 根据类型查询分类
     * @param type 分类
     * @return R
     */
    @ApiOperation(value = "根据类型查询分类")
    @GetMapping("/list")
    public Result selectByType(Integer type){
        List<Category> categories = categoryService.selectByType(type);
        return Result.success(categories);
    }
}
